package com.beiing.tianshuai.tianshuai.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.AddFriendsBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.presenter.AddFriendsPresenter;
import com.beiing.tianshuai.tianshuai.view.AddFriendsViewImpl;
import com.beiing.tianshuai.tianshuai.widget.CustomProgressDialog;
import com.netease.nim.uikit.common.util.log.LogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 请求添加好友
 */
public class RequestAddFriendActivity extends BaseActivity implements AddFriendsViewImpl {

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.toolbar_btn_publish)
    TextView mToolbarBtnPublish;
    @BindView(R.id.edit_request_message)
    EditText mEditRequestMessage;

    private static final String TAG = "RequestAddFriend";

    private Unbinder mUnbinder;

    private Context mContext;

    private String mUid;

    private AddFriendsPresenter mPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_request_add_friend;
    }

    @Override
    protected void init() {
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        Intent intent = getIntent();
        mUid = intent.getStringExtra("uid");
        initToolbar();
        initPresenter();
        initListener();
    }

    private void initToolbar() {
        mToolbarIvBack.setVisibility(View.VISIBLE);
        mToolbarBtnPublish.setVisibility(View.VISIBLE);
        mToolbarTvTitle.setText("验证申请");
    }

    private void initPresenter() {
        mPresenter = new AddFriendsPresenter(this);
    }

    private void initListener() {
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mToolbarBtnPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String message = mEditRequestMessage.getText().toString();
                if (TextUtils.isEmpty(message)) {
                    message = "我是" + UserInfoBean.getNickName(mContext);
                }
                mPresenter.getAddFriendsResult(UserInfoBean.getUid(mContext), mUid, message);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

    @Override
    public void showProgress() {
        CustomProgressDialog.showLoading(mContext, "正在发送请求，请稍后...", true);
    }

    @Override
    public void hideProgress() {
        CustomProgressDialog.stopLoading();
    }

    @Override
    public void onRequestSuccess(AddFriendsBean bean) {
        int code = bean.getCode();
        switch (code) {
            case 200:
                showShortText("发送成功");
                break;
            case 206:
                showShortText("已经请求过了，请耐心等待哦");
                break;
            case 407:
                showShortText("你们已经是好友了哦");
                break;
            case 406:
                showShortText("登录超时");
                break;
            case 201:
                showShortText("已请求!");
            default:break;
        }
        finish();
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        showShortText("网络异常"+throwable.getMessage());
        LogUtil.i(TAG,"网络异常"+throwable.getMessage()+mUid+" "+UserInfoBean.getUid(mContext));
    }
}
